﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using Cat;
using MyDBconnection;

namespace Thesis_2
{
    public partial class frmCategory : Form
    {
        ConnectionString cs = new ConnectionString();
        OdbcConnection mycon = null;
        OdbcCommand cmd = null;
        OdbcDataReader rdr = null;
        DatabaseConnection dbconn = new DatabaseConnection();
        public frmCategory()
        {
            InitializeComponent();
            this.Load += new EventHandler(frmCategory_Load);
            btnSave.Click += new EventHandler(btnSave_Click);
            btnGetData.Click += new EventHandler(btnGetData_Click);
            btnNew.Click += new EventHandler(btnNew_Click);
            btnUpdate.Click += new EventHandler(btnUpdate_Click);
        }

        void frmCategory_Load(object sender, EventArgs e)
        {
            
        }
        void btnUpdate_Click(object sender, EventArgs e)
        {
            if (txtCategoryName.Text == "")
            {
                MessageBox.Show("Please Enter Category Name","Error", MessageBoxButtons.OK,MessageBoxIcon.Error);
                txtCategoryName.Focus();
                return;
            }
            dbconn.dbconnection();
            dbconn.UpdateData("Update tblcategory set CategoryName='" + txtCategoryName.Text + "' Where CategoryName='" + textBox1.Text + "';");
            MessageBox.Show("Update Success", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            reset();
            dbconn.DBClose();   
        }

        void btnNew_Click(object sender, EventArgs e)
        {
            reset();
        }       
        void btnGetData_Click(object sender, EventArgs e)
        {
            frmCategorylist frmC =  new frmCategorylist();
            frmC.Show();
            this.Hide();

        }
        void btnSave_Click(object sender, EventArgs e)
        {

            if (txtCategoryName.Text == "")
            {
                MessageBox.Show("Please enter Category name", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

                txtCategoryName.Focus();
                return;
            }

            try
            {

                mycon = new OdbcConnection(cs.DBConn);
                mycon.Open();
                string ct = "Select CategoryName from tblcategory where CategoryName = '" + txtCategoryName.Text + "'";
                cmd = new OdbcCommand(ct);
                cmd.Connection = mycon;
                rdr = cmd.ExecuteReader();

                if (rdr.Read())
                {
                    MessageBox.Show("Category Name Already Exists", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    txtCategoryName.Text = "";
                    txtCategoryName.Focus();


                    if ((rdr != null))
                    {
                        rdr.Close();
                    }
                    return;
                }

                mycon = new OdbcConnection(cs.DBConn);
                mycon.Open();

                string cb = "Insert into tblcategory(CategoryName) VALUES ('" + txtCategoryName.Text + "')";

                cmd = new OdbcCommand(cb);
                cmd.Connection = mycon;
                cmd.ExecuteReader();
                mycon.Close();
                MessageBox.Show("Successfully saved", "Record", MessageBoxButtons.OK, MessageBoxIcon.Information);
                reset();
                btnSave.Enabled = false;

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        public void reset()
        {
            txtCategoryName.Text = "";
            btnSave.Enabled = true;
            btnUpdate.Enabled = false;
        }
    }
}
